package com.blkbox.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class BatchRenameFile {
	private static String FILE_NAME_ORI_STR = "EXT";
	private static String FILE_NAME_NEW_STR = "BVA";
	private static String APPLN_TYPE_ORI_STR = "APPLICATION FOR EXTENSION TO VEHICLE TYPE APPROVAL";
	private static String FILE_CONTENT_NEW_STR = "APPLICATION FOR BATCH VEHICLE APPROVAL";

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		renameFiles();
	}

	private static void renameFiles() throws IOException {
		String dirPath = "D:/Develop/svn/client/vitas_mockup/trunk/Mockup Pages_v9/All Application/lta_process/bva/";
		File dir = new File(dirPath);

		for (File oldFile : dir.listFiles()) {
			String oldFileName = oldFile.getName();
			String newFileName = oldFileName.replace(FILE_NAME_ORI_STR, FILE_NAME_NEW_STR);
			File newFile = new File(dirPath + newFileName);
			if (newFile.exists()) {
				throw new IOException("file exists");
			} else {
				processFile(oldFile, newFile);
			}

			oldFile.delete();
			/*
			 * boolean success = oldFile.renameTo(newFile); if (!success) {
			 * System.out.println("Error!!"); }
			 */
		}

	}

	private static void processFile(File oldfile, File newFile) throws IOException {
		// ArrayList<String> lines = new ArrayList<String>();

		String fullFileName = oldfile.getPath();
		BufferedReader br = new BufferedReader(new FileReader(fullFileName));
		BufferedWriter bw = new BufferedWriter(new FileWriter(newFile.getPath()));
		String tempStr = null;
		try {
			while ((tempStr = br.readLine()) != null) {
				tempStr = tempStr.replace(APPLN_TYPE_ORI_STR, FILE_CONTENT_NEW_STR).replace("ES2013020000", "ES2013030000")
						.replace("20130200000", "20130300000").replace("Process" + FILE_NAME_ORI_STR, "Process" + FILE_NAME_NEW_STR);
				// lines.add(tempStr);
				bw.write(tempStr + "\n");
			}
		} catch (IOException e) {
			System.out.println("Read File Error!");
		}
		br.close();
		bw.close();

	}

}
